package com.sunmi.paymentdemo.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sunmi.paymentdemo.BuildConfig;
import com.sunmi.paymentdemo.R;


public class CustomRadioButton extends LinearLayout {
    private TextView radio_title, radio_indicator;
    private RadioButton radio_content, radio_uncontent;

    private CheckBox mCheckBox;

    public CustomRadioButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomRadioButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_radiobutton, this, true);
        radio_title = findViewById(R.id.radio_title);
        radio_content = findViewById(R.id.radio_content);
        radio_uncontent = findViewById(R.id.radio_uncontent);
        radio_indicator = findViewById(R.id.radio_indicator);
        mCheckBox = findViewById(R.id.checkbox_upload);
        //测试版本才显示勾选框
        if (BuildConfig.BUILD_TYPE.equals("envTest")) {
            mCheckBox.setVisibility(VISIBLE);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setText(int title, int indicator) {
        radio_title.setText(title);
        radio_indicator.setText(indicator);
    }

    public void setTitle(int firstTile, int secondTitle) {
        radio_content.setText(firstTile);
        radio_uncontent.setText(secondTitle);
    }

    public Boolean getContent() {
        if (isUpload()) {
            return radio_content.isChecked();
        } else {
            return null;
        }
    }

    public void setChecked(boolean checked) {
        radio_content.setChecked(checked);
        radio_uncontent.setChecked(!checked);
    }

    /**
     * 是否勾选
     *
     * @return
     */
    private boolean isUpload() {
        if (BuildConfig.BUILD_TYPE.equals("envTest")) {
            return mCheckBox.isChecked();
        } else {
            return true;
        }
    }
}
